package com.shsoft.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.DialogFragment;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.colorfulcc.R;
import com.shsoft.common.Constant;
import com.shsoft.entity.CompositeItem;
import com.shsoft.utils.MessageUtil;

import org.angmarch.views.NiceSpinner;


public class CompositeEditFragment extends DialogFragment implements TextView.OnFocusChangeListener,TextView.OnEditorActionListener,View.OnClickListener{
    private MainActivity activity;
    private CompositeItem item;
    private NiceSpinner compositeModeSpinner,breathingModeSpinner,flowingWaterModeSpinner;
    private TextView title,submit,exit,long_circle_tips,horse_circle_tips,flowingWater_circle_tips,breathing_circle_tips;
    private ScrollView longMode,horse,flowingWater,breathing;
    private int breathingMode; // 记录呼吸模式
    private int flowingWaterMode; // 记录流水模式
    private EditText long_circle,long_bright, long_interval, long_offtime,horse_bright,horse_count,horse_speed,horse_circle,flowingWater_bright,flowingWater_speed,flowingWater_circle,breathing_time,breathing_circle,breathing_bright; // 具体参数
    private LinearLayout tips, colorDiv, long_circle_div,long_bright_div, long_interval_div, long_offtime_div,horse_bright_div,horse_count_div,horse_direction_div,horse_speed_div,horse_circle_div,flowingWater_bright_div,flowingWater_direction_div,flowingWater_speed_div,flowingWater_circle_div,breathing_time_div,breathing_circle_div,breathing_bright_div; // 参数区div
    private RadioGroup horse_direction,flowingWater_direction;
    private RadioButton horse_clockwise,horse_anticlockwise,flowingWater_clockwise,flowingWater_anticlockwise;
    private int horse_direction_value ; // 跑马灯方向
    private int flowingWater_direction_value; // 流水灯方向
    private CardView color1, color2, color3, color4, color5, color6, color7, color8, color9, color10,color11, color12, color13, color14, color15, color16, color17, color18, color19, color0;
    private TextView colorRecord; // 记录纯色模式下选择了哪些颜色编号

    public CompositeEditFragment(MainActivity activity,CompositeItem item) {
        this.activity = activity;
        this.item = item;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        View view = inflater.inflate(R.layout.composite_edit_fragment, container, false);
        //初始化控件
        title = view.findViewById(R.id.title);
        colorDiv = view.findViewById(R.id.colorDiv);
        colorRecord = view.findViewById(R.id.colorRecord);
        submit = view.findViewById(R.id.submit);
        submit.setOnClickListener(view12 -> {
            if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                if(long_bright.getText().toString().equals("") || long_interval.getText().toString().equals("") || long_offtime.getText().toString().equals("")){
                    MessageUtil.toastShort(activity, "请完善参数");
                    return;
                }
                saveItem();
            }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                if(horse_bright.getText().toString().equals("") || horse_speed.getText().toString().equals("")|| horse_count.getText().toString().equals("")
                        || horse_circle.getText().toString().equals("")){
                    MessageUtil.toastShort(activity, "请完善参数");
                    return;
                }
                saveItem();
            }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                if(flowingWater_bright.getText().toString().equals("") || flowingWater_speed.getText().toString().equals("")|| flowingWater_circle.getText().toString().equals("")){
                    MessageUtil.toastShort(activity, "请完善参数");
                    return;
                }
                saveItem();
            }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                if(breathing_time.getText().toString().equals("") || breathing_circle.getText().toString().equals("")|| breathing_bright.getText().toString().equals("")){
                    MessageUtil.toastShort(activity, "请完善参数");
                    return;
                }
                saveItem();
            }
        });
        exit = view.findViewById(R.id.exit);
        exit.setOnClickListener(view1 -> {
            this.dismiss();
            activity.isCompositeAddOpened = false;
        });
        initOptions(view);
        bindOptions();
        compositeModeSpinner = view.findViewById(R.id.compositeModeSpinner);
        compositeModeSpinner.setBackgroundResource(R.drawable.composite_add_spinner_border);
        compositeModeSpinner.setOnSpinnerItemSelectedListener((parent, view13, position, id) -> {
            String item = (String) parent.getItemAtPosition(position);
            switch (item){
                case "常亮":
                    // 将所有的设置区都先不可见
                    invisibleAllOptions();
                    // 设置常亮可见
                    longMode.setVisibility(View.VISIBLE);
                    // 修改lightMode
                    this.item.setLightMode(Constant.LIGHT_MODE_LONG);
                    break;
                case "跑马":
                    // 将所有的设置区都先不可见
                    invisibleAllOptions();
                    // 设置跑马可见
                    horse.setVisibility(View.VISIBLE);
                    // 修改lightMode
                    this.item.setLightMode(Constant.LIGHT_MODE_HORSE);
                    break;
                case "流水":
                    // 将所有的设置区都先不可见
                    invisibleAllOptions();
                    // 设置流水可见
                    flowingWater.setVisibility(View.VISIBLE);
                    // 修改lightMode
                    this.item.setLightMode(Constant.LIGHT_MODE_FLOWING_WATER);
                    break;
                case "呼吸":
                    // 将所有的设置区都先不可见
                    invisibleAllOptions();
                    // 设置呼吸可见
                    breathing.setVisibility(View.VISIBLE);
                    // 修改lightMode
                    this.item.setLightMode(Constant.LIGHT_MODE_BREATHING);
                    break;
                default:
                    break;
            }
        });
        breathingModeSpinner = view.findViewById(R.id.breathingModeSpinner);
        breathingModeSpinner.setBackgroundResource(R.drawable.composite_add_spinner_border);
        breathingModeSpinner.setOnSpinnerItemSelectedListener((parent, view13, position, id) -> {
            String item = (String) parent.getItemAtPosition(position);
            switch (item){
                case "暗→明→暗":
                    breathingMode = Constant.BREATHING_MODE_D_L_D;
                    break;
                case "明→暗→明":
                    breathingMode = Constant.BREATHING_MODE_L_D_L;
                    break;
                case "明→暗":
                    breathingMode = Constant.BREATHING_MODE_L_D;
                    break;
                case "暗→明":
                    breathingMode = Constant.BREATHING_MODE_D_L;
                    break;
                default:
                    break;
            }
        });
        flowingWaterModeSpinner = view.findViewById(R.id.flowingWaterModeSpinner);
        flowingWaterModeSpinner.setBackgroundResource(R.drawable.composite_add_spinner_border);
        flowingWaterModeSpinner.setOnSpinnerItemSelectedListener((parent, view13, position, id) -> {
            String item = (String) parent.getItemAtPosition(position);
            switch (item){
                case "尾部跟进":
                    flowingWaterMode = Constant.FLOWINGWATER_MODE_TAIL_FOLLOW;
                    break;
                case "头部回退":
                    flowingWaterMode = Constant.FLOWINGWATER_MODE_HEAD_BACK;
                    break;
                case "灯满结束":
                    flowingWaterMode = Constant.FLOWINGWATER_MODE_FULL_FINISH;
                    break;
                default:
                    break;
            }
        });
        // 处理哪些控件该显示，哪些不显示
        doDisplay(view);
        return view;
    }

    //设置fragment的大小
    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.5f;//透明度
        windowParams.y = 100;
        window.setAttributes(windowParams);
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.95), (int) (dm.heightPixels * 0.6));
        }
    }

    // 保存数据
    private void saveItem(){
        // 先得到剩余字节数,用来一会做比较，如果一会要保存的字节数大于这个，就给用户提示，并且不保存
        int remain = Integer.parseInt(activity.remain.getText().toString());
        int currentByteCount = item.getByteCount(); // 当前item原本的字节数
        // 如果是纯色模式，获取已选择的颜色编号
        String s = "";
        int colorCount = 0;
        if(item.getColorMode() == Constant.COLOR_MODE_SINGLE){ // 纯色模式
            s = colorRecord.getText().toString();
            if(s.equals("")){
                MessageUtil.toastShort(activity,"请至少选择一种颜色");
                return;
            }
            String[] split = s.split(","); // 分割出有多少种颜色
            colorCount = split.length;
        }else { // 混色模式，20种颜色编号都放进去
            for (int i = 0; i < Constant.LIGHT_COUNT; i++) {
                s = s + "#" + activity.lights.get(i) + ",";
            }
        }
        switch (item.getLightMode()){
            case Constant.LIGHT_MODE_LONG:
                item.setArg1(Integer.parseInt(long_bright.getText().toString()));
                item.setArg2(Integer.parseInt(long_interval.getText().toString()));
                item.setArg3(Integer.parseInt(long_offtime.getText().toString()));
                item.setArg4(Integer.parseInt(long_circle.getText().toString()));
                item.setColorNum(s);
                if(item.getColorMode() == Constant.COLOR_MODE_MIX){
                    item.setByteCount(25); // 花费25字节    1.亮灯模式 2.亮度 3.亮灯时间 4.灭灯时间 5.轮数 6-25字节每个灯的颜色编号
                }else {
                    item.setByteCount(6 + colorCount); // 纯色模式多一个结束字节
                }
                break;
            case Constant.LIGHT_MODE_HORSE:
                item.setArg1(Integer.parseInt(horse_bright.getText().toString()));
                item.setArg2(horse_direction_value);
                item.setArg3(Integer.parseInt(horse_speed.getText().toString()));
                item.setArg4(Integer.parseInt(horse_circle.getText().toString()));
                item.setColorNum(s);
                if(item.getColorMode() == Constant.COLOR_MODE_MIX){
                    item.setByteCount(25); // 花费25字节   1.亮灯模式 2.亮度 3.方向 4.跑马速度 5.圈数  6-25字节每个灯的颜色编号
                }else {
                    item.setArg5(Integer.parseInt(horse_count.getText().toString()));
                    item.setByteCount(7 + colorCount); // 纯色模式多一个结束字节和灯珠数量
                }
                break;
            case Constant.LIGHT_MODE_FLOWING_WATER:
                item.setArg1(Integer.parseInt(flowingWater_bright.getText().toString()));
                item.setArg2(flowingWater_direction_value);
                item.setArg3(Integer.parseInt(flowingWater_speed.getText().toString()));
                item.setArg4(Integer.parseInt(flowingWater_circle.getText().toString()));
                item.setArg5(flowingWaterMode);
                item.setColorNum(s);
                if(item.getColorMode() == Constant.COLOR_MODE_MIX){
                    item.setByteCount(26); // 花费26字节   1.亮灯模式 2.亮度 3.方向 4.流水速度 5.圈数  6.流水方式 7-26字节每个灯的颜色编号
                }else {
                    item.setByteCount(7 + colorCount); // 纯色模式多一个结束字节
                }
                break;
            case Constant.LIGHT_MODE_BREATHING:
                item.setArg1(Integer.parseInt(breathing_bright.getText().toString()));
                item.setArg2(Integer.parseInt(breathing_time.getText().toString()));
                item.setArg3(Integer.parseInt(breathing_circle.getText().toString()));
                item.setArg4(breathingMode);
                item.setColorNum(s);
                if(item.getColorMode() == Constant.COLOR_MODE_MIX){
                    item.setByteCount(25); // 花费25字节   1.亮灯模式 2.亮度 3.呼吸时间 4.呼吸次数 5.呼吸模式 6-25字节每个灯的颜色编号
                }else {
                    item.setByteCount(6 + colorCount); // 纯色模式多一个结束字节
                }
                break;
            default:
                break;
        }
        if((remain + currentByteCount) < item.getByteCount()){ // 剩余字节数不足
            MessageUtil.toastShort(activity,"剩余字节数不足");
            return;
        }
        if(item.save()){ // 保存成功
            // 将主页面重新设置字节剩余数
            activity.setRemain();
            // 刷新控件
            activity.updateRecycleView();
            // 关闭自己
            this.dismiss();
            activity.isCompositeAddOpened = false;
        }
    }

    // 处理哪些控件该显示，哪些不显示
    private void doDisplay(View view){
        // 显示当前亮灯模式的选项
        invisibleAllOptions();
        if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
            longMode.setVisibility(View.VISIBLE);
            compositeModeSpinner.setSelectedIndex(0);
            long_bright.setText(item.getArg1() + "");
            long_interval.setText(item.getArg2() + "");
            long_offtime.setText(item.getArg3() + "");
            long_circle.setText(item.getArg4() + "");
        }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
            horse.setVisibility(View.VISIBLE);
            compositeModeSpinner.setSelectedIndex(1);
            horse_bright.setText(item.getArg1() + "");
            if(item.getArg2() == Constant.CLOCKWISE){
                horse_clockwise.setChecked(true);
                horse_direction_value = Constant.CLOCKWISE;
            }else{
                horse_anticlockwise.setChecked(true);
                horse_direction_value = Constant.ANTICLOCKWISE;
            }
            horse_speed.setText(item.getArg3() + "");
            horse_circle.setText(item.getArg4() + "");
            if(item.getColorMode() == Constant.COLOR_MODE_SINGLE){
                horse_count.setText(item.getArg5() + "");
            }
        }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
            flowingWater.setVisibility(View.VISIBLE);
            compositeModeSpinner.setSelectedIndex(2);
            flowingWater_bright.setText(item.getArg1() + "");
            if(item.getArg2() == Constant.CLOCKWISE){
                flowingWater_clockwise.setChecked(true);
                flowingWater_direction_value = Constant.CLOCKWISE;
            }else{
                flowingWater_anticlockwise.setChecked(true);
                flowingWater_direction_value = Constant.ANTICLOCKWISE;
            }
            flowingWater_speed.setText(item.getArg3() + "");
            flowingWater_circle.setText(item.getArg4() + "");
            flowingWaterMode = item.getArg5();
            flowingWaterModeSpinner.setSelectedIndex(flowingWaterMode - 1);
        }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
            breathing.setVisibility(View.VISIBLE);
            compositeModeSpinner.setSelectedIndex(3);
            breathing_bright.setText(item.getArg1() + "");
            breathing_time.setText(item.getArg2() + "");
            breathing_circle.setText(item.getArg3() + "");
            breathingMode = item.getArg4();
            breathingModeSpinner.setSelectedIndex(breathingMode - 1);
        }
        if(item.getColorMode() == Constant.COLOR_MODE_SINGLE){
            title.setText("纯色模式修改步骤");
            horse_count_div.setVisibility(View.VISIBLE);
            colorDiv.setVisibility(View.VISIBLE);
            tips.setVisibility(View.INVISIBLE);
            horse_circle_tips.setText("单色圈数(1-250)");
            flowingWater_circle_tips.setText("单色圈数(1-250)");
            breathing_circle_tips.setText("单色次数(1-250)");
            long_circle_tips.setText("单色轮数(1-250)");
            // 显示具体的颜色
            colorRecord.setText(item.getColorNum());
            CardView colorZone;
            int colorId;
            int[] colorRGB;
            for (int i = 0; i < activity.colors.size(); i++){
                colorId = getResources().getIdentifier("color"+ i,"id",activity.getPackageName());
                colorZone = view.findViewById(colorId);
                colorZone.setVisibility(View.VISIBLE);
                colorRGB = activity.colors.get(i);
                if(item.getColorNum().contains("#" + i + ",")){
                    colorZone.setCardBackgroundColor(Color.argb(50, colorRGB[0],colorRGB[1],colorRGB[2]));
                }else {
                    colorZone.setCardBackgroundColor(Color.rgb(colorRGB[0],colorRGB[1],colorRGB[2]));
                }
            }
        }else {
            title.setText("混色模式修改步骤");
            horse_count_div.setVisibility(View.GONE);
            colorDiv.setVisibility(View.GONE);
            tips.setVisibility(View.VISIBLE);
            horse_circle_tips.setText("圈数(1-250)");
            flowingWater_circle_tips.setText("圈数(1-250)");
            breathing_circle_tips.setText("呼吸次数(1-250)");
            long_circle_tips.setText("轮数(1-250)");
        }
    }

    // 将参数区所有的模式都不可见
    private void invisibleAllOptions(){
        longMode.setVisibility(View.GONE);
        horse.setVisibility(View.GONE);
        flowingWater.setVisibility(View.GONE);
        breathing.setVisibility(View.GONE);
    }

    // 初始化参数区的控件
    private void initOptions(View view){
        longMode = view.findViewById(R.id.longMode);
        horse = view.findViewById(R.id.horse);
        flowingWater = view.findViewById(R.id.flowingWater);
        breathing = view.findViewById(R.id.breathing);
        // 常亮区参数
        long_bright = view.findViewById(R.id.long_bright);
        long_interval = view.findViewById(R.id.long_interval);
        long_offtime = view.findViewById(R.id.long_offtime);
        long_circle = view.findViewById(R.id.long_circle);
        long_bright_div = view.findViewById(R.id.long_bright_div);
        long_interval_div = view.findViewById(R.id.long_interval_div);
        long_offtime_div = view.findViewById(R.id.long_offtime_div);
        long_circle_div = view.findViewById(R.id.long_circle_div);
        long_circle_tips = view.findViewById(R.id.long_circle_tips);
        // 跑马区
        horse_bright_div = view.findViewById(R.id.horse_bright_div);
        horse_count_div = view.findViewById(R.id.horse_count_div);
        horse_direction_div = view.findViewById(R.id.horse_direction_div);
        horse_circle_div = view.findViewById(R.id.horse_circle_div);
        horse_speed_div = view.findViewById(R.id.horse_speed_div);
        horse_bright = view.findViewById(R.id.horse_bright);
        horse_count = view.findViewById(R.id.horse_count);
        horse_direction = view.findViewById(R.id.horse_direction);
        horse_circle = view.findViewById(R.id.horse_circle);
        horse_speed = view.findViewById(R.id.horse_speed);
        horse_clockwise = view.findViewById(R.id.horse_clockwise);
        horse_anticlockwise = view.findViewById(R.id.horse_anticlockwise);
        horse_circle_tips = view.findViewById(R.id.horse_circle_tips);
        // 流水区
        flowingWater_bright_div = view.findViewById(R.id.flowingWater_bright_div);
        flowingWater_direction_div = view.findViewById(R.id.flowingWater_direction_div);
        flowingWater_speed_div = view.findViewById(R.id.flowingWater_speed_div);
        flowingWater_circle_div = view.findViewById(R.id.flowingWater_circle_div);
        flowingWater_bright = view.findViewById(R.id.flowingWater_bright);
        flowingWater_direction = view.findViewById(R.id.flowingWater_direction);
        flowingWater_speed = view.findViewById(R.id.flowingWater_speed);
        flowingWater_circle = view.findViewById(R.id.flowingWater_circle);
        flowingWater_clockwise = view.findViewById(R.id.flowingWater_clockwise);
        flowingWater_anticlockwise = view.findViewById(R.id.flowingWater_anticlockwise);
        flowingWater_circle_tips = view.findViewById(R.id.flowingWater_circle_tips);
        // 呼吸区
        breathing_time_div = view.findViewById(R.id.breathing_time_div);
        breathing_circle_div = view.findViewById(R.id.breathing_circle_div);
        breathing_bright_div = view.findViewById(R.id.breathing_bright_div);
        breathing_time = view.findViewById(R.id.breathing_time);
        breathing_circle = view.findViewById(R.id.breathing_circle);
        breathing_bright = view.findViewById(R.id.breathing_bright);
        breathing_circle_tips = view.findViewById(R.id.breathing_circle_tips);
        // 颜色区
        color0 = view.findViewById(R.id.color0);
        color1 = view.findViewById(R.id.color1);
        color2 = view.findViewById(R.id.color2);
        color3= view.findViewById(R.id.color3);
        color4 = view.findViewById(R.id.color4);
        color5 = view.findViewById(R.id.color5);
        color6 = view.findViewById(R.id.color6);
        color7 = view.findViewById(R.id.color7);
        color8 = view.findViewById(R.id.color8);
        color9 = view.findViewById(R.id.color9);
        color10 = view.findViewById(R.id.color10);
        color11 = view.findViewById(R.id.color11);
        color12 = view.findViewById(R.id.color12);
        color13 = view.findViewById(R.id.color13);
        color14 = view.findViewById(R.id.color14);
        color15 = view.findViewById(R.id.color15);
        color16 = view.findViewById(R.id.color16);
        color17 = view.findViewById(R.id.color17);
        color18 = view.findViewById(R.id.color18);
        color19 = view.findViewById(R.id.color19);
        // 提示文字区
        tips = view.findViewById(R.id.tips);
    }

    // 绑定参数区的事件
    private void bindOptions(){
        // 常亮区
        long_bright.setOnFocusChangeListener(this);
        long_interval.setOnFocusChangeListener(this);
        long_offtime.setOnFocusChangeListener(this);
        long_circle.setOnFocusChangeListener(this);
        long_bright.setOnEditorActionListener(this);
        long_interval.setOnEditorActionListener(this);
        long_offtime.setOnEditorActionListener(this);
        long_circle.setOnEditorActionListener(this);
        // 跑马区
        horse_bright.setOnFocusChangeListener(this);
        horse_count.setOnFocusChangeListener(this);
        horse_circle.setOnFocusChangeListener(this);
        horse_speed.setOnFocusChangeListener(this);
        horse_bright.setOnEditorActionListener(this);
        horse_count.setOnEditorActionListener(this);
        horse_circle.setOnEditorActionListener(this);
        horse_speed.setOnEditorActionListener(this);
        horse_direction.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if(checkedId == R.id.horse_clockwise){
                    horse_direction_value = Constant.CLOCKWISE;
                }else if(checkedId == R.id.horse_anticlockwise){
                    horse_direction_value = Constant.ANTICLOCKWISE;
                }
            }
        });
        // 流水区
        flowingWater_bright.setOnFocusChangeListener(this);
        flowingWater_speed.setOnFocusChangeListener(this);
        flowingWater_circle.setOnFocusChangeListener(this);
        flowingWater_bright.setOnEditorActionListener(this);
        flowingWater_speed.setOnEditorActionListener(this);
        flowingWater_circle.setOnEditorActionListener(this);
        flowingWater_direction.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if(checkedId == R.id.flowingWater_clockwise){
                    flowingWater_direction_value = Constant.CLOCKWISE;
                }else if(checkedId == R.id.flowingWater_anticlockwise){
                    flowingWater_direction_value = Constant.ANTICLOCKWISE;
                }
            }
        });
        // 呼吸区
        breathing_time.setOnFocusChangeListener(this);
        breathing_circle.setOnFocusChangeListener(this);
        breathing_bright.setOnFocusChangeListener(this);
        breathing_time.setOnEditorActionListener(this);
        breathing_circle.setOnEditorActionListener(this);
        breathing_bright.setOnEditorActionListener(this);
        // 颜色区
        color0.setOnClickListener(this);
        color1.setOnClickListener(this);
        color2.setOnClickListener(this);
        color3.setOnClickListener(this);
        color4.setOnClickListener(this);
        color5.setOnClickListener(this);
        color6.setOnClickListener(this);
        color7.setOnClickListener(this);
        color8.setOnClickListener(this);
        color9.setOnClickListener(this);
        color10.setOnClickListener(this);
        color11.setOnClickListener(this);
        color12.setOnClickListener(this);
        color13.setOnClickListener(this);
        color14.setOnClickListener(this);
        color15.setOnClickListener(this);
        color16.setOnClickListener(this);
        color17.setOnClickListener(this);
        color18.setOnClickListener(this);
        color19.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.color0:
                clickColor(view,0);
                break;
            case R.id.color1:
                clickColor(view,1);
                break;
            case R.id.color2:
                clickColor(view,2);
                break;
            case R.id.color3:
                clickColor(view,3);
                break;
            case R.id.color4:
                clickColor(view,4);
                break;
            case R.id.color5:
                clickColor(view,5);
                break;
            case R.id.color6:
                clickColor(view,6);
                break;
            case R.id.color7:
                clickColor(view,7);
                break;
            case R.id.color8:
                clickColor(view,8);
                break;
            case R.id.color9:
                clickColor(view,9);
                break;
            case R.id.color10:
                clickColor(view,10);
                break;
            case R.id.color11:
                clickColor(view,11);
                break;
            case R.id.color12:
                clickColor(view,12);
                break;
            case R.id.color13:
                clickColor(view,13);
                break;
            case R.id.color14:
                clickColor(view,14);
                break;
            case R.id.color15:
                clickColor(view,15);
                break;
            case R.id.color16:
                clickColor(view,16);
                break;
            case R.id.color17:
                clickColor(view,17);
                break;
            case R.id.color18:
                clickColor(view,18);
                break;
            case R.id.color19:
                clickColor(view,19);
                break;
            default:
                break;
        }
    }

    // 点击颜色事件
    private void clickColor(View view, int num){
        // 获取colorRecord的值，看看有没有这个值
        String snum = "#" + num + ",";
        String s = colorRecord.getText().toString();
        int[] colorRGB = activity.colors.get(num);
        CardView cardView = (CardView) view;
        String snew;
        if(s.contains(snum)){ // 已经包含了这个值,则删除
            snew = s.replace(snum, "");
            cardView.setCardBackgroundColor(Color.rgb(colorRGB[0],colorRGB[1],colorRGB[2]));
        }else {
            snew = s + snum;
            cardView.setCardBackgroundColor(Color.argb(50, colorRGB[0],colorRGB[1],colorRGB[2]));
        }
        colorRecord.setText(snew);
        MessageUtil.logDebug(colorRecord.getText().toString());
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if(!b){ // 失去焦点
            switch (view.getId()){
                case R.id.long_bright:
                case R.id.horse_bright:
                case R.id.flowingWater_bright:
                case R.id.breathing_bright:
                    doLostFocus(view, "100", 1,250);
                    break;
                case R.id.long_interval:
                    doLostFocus(view, "10", 0,250);
                    break;
                case R.id.long_offtime:
                    doLostFocus(view, "0", 0,250);
                    break;
                case R.id.horse_count:
                    doLostFocus(view, "1", 1,40);
                    break;
                case R.id.horse_circle:
                case R.id.flowingWater_circle:
                case R.id.breathing_circle:
                case R.id.long_circle:
                    doLostFocus(view, "1", 1,250);
                    break;
                case R.id.horse_speed:
                case R.id.flowingWater_speed:
                    doLostFocus(view, "10", 1,250);
                    break;
                case R.id.breathing_time:
                    doLostFocus(view, "2", 1,250);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if(i == EditorInfo.IME_ACTION_DONE){
            textView.clearFocus();
            return true;
        }
        return false;
    }

    // 处理edittext失去焦点事件
    private void doLostFocus(View view, String defultValue, int minValue, int maxValue){
        // 强制转为 EditText
        EditText editText = (EditText) view;
        // 如果什么值都没填，使用默认值填上
        String text = editText.getText().toString();
        if(text.equals("")){
            editText.setText(defultValue);
            return;
        }
        // 转为int并判断，将值设为1-250之间
        int i = Integer.parseInt(text);
        if(i < minValue){
            editText.setText(minValue + "");
        }else if(i > maxValue){
            editText.setText(maxValue + "");
        }
    }
}
